import {createRouter, createWebHistory} from 'vue-router';
import {ElMessage} from 'element-plus';

declare module 'vue-router' {
  interface RouteMeta {
    title?: string;
    sidebar?: SidebarType;
  }
}

// eslint-disable-next-line no-shadow
export enum SidebarType {
  admin = 'admin',
  normal = 'normal',
}
const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('../views/index.vue'),
    children: [
      {
        path: '/',
        name: 'home',
        component: () => import('../views/home/index.vue'),
        meta: {
          title: '首页',
        },
      },
      {
        path: '/comprehensiveCensus',
        name: 'comprehensiveCensus',
        component: () => import('../views/census/comprehensiveCensus.vue'),
        meta: {
          title: '综合统计',
        },
      },

      {
        path: '/flightDataDetail',
        name: 'flightDataDetail',
        component: () => import('../views/census/flightDataDetail.vue'),
        meta: {
          title: '飞行数据统计详情',
        },
      },
      {
        path: '/applyManage',
        name: 'applyManage',
        component: () => import('../views/manage/apply/applyManage.vue'),
        meta: {
          title: '申请管理',
          sidebar: SidebarType.admin,
        },
      },
      {
        path: '/configManage',
        name: 'configManage',
        component: () => import('../views/manage/permission/configManage.vue'),
        meta: {
          title: '配置管理',
          sidebar: SidebarType.admin,
        },
      },
      {
        path: '/permissionManage',
        name: 'permissionManage',
        component: () => import('../views/manage/permission/permissionManage.vue'),
        meta: {
          title: '权限管理',
          sidebar: SidebarType.admin,
        },
      },
      {
        path: '/userManage',
        name: 'userManage',
        component: () => import('../views/manage/user/userManage.vue'),
        meta: {
          title: '用户管理',
          sidebar: SidebarType.normal,
        },
      },
      {
        path: '/myInfo',
        name: 'myInfo',
        component: () => import('../views/manage/myInfo/myInfo.vue'),
        meta: {
          title: '个人中心',
          sidebar: SidebarType.normal,
        },
      },
      {
        path: '/account',
        name: 'account',
        component: () => import('../views/manage/account/accountSettings.vue'),
        meta: {
          title: '账户设置',
          sidebar: SidebarType.normal,
        },
      },
      {
        path: '/msgCenter',
        name: 'msgCenter',
        component: () => import('../views/manage/msgCenter/msgCenter.vue'),
        meta: {
          title: '消息中心',
          sidebar: SidebarType.normal,
        },
      },
      {
        path: '/unitManage',
        name: 'unitManage',
        component: () => import('../views/manage/unit/unitManage.vue'),
        meta: {
          title: '单位管理',
          sidebar: SidebarType.normal,
        },
      },
      {
        path: '/deviceManage',
        name: 'deviceManage',
        component: () => import('../views/manage/device/deviceManage.vue'),
        meta: {
          title: '设备管理',
          sidebar: SidebarType.normal,
        },
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue'),
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register.vue'),
  },
  {
    path: '/forgetPassword',
    name: 'forgetPassword',
    component: () => import('../views/forgetPassword.vue'),
  },
  {
    path: '/companyLogin',
    name: 'companyLogin',
    component: () => import('../views/companyLogin.vue'),
    meta: {
      title: '单位登录成功',
    },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) {
    // 如果未匹配到路由
    ElMessage.warning({
      message: '未找到匹配的路由，请检查路由地址是否正确',
      type: 'warning',
    });
    router.push({
      path: '/login',
    });
  } else {
    // 若未登陆时使用其他地址进入网站，进行token验证，判断是否存在token,不存在则返回登陆页面
    const token = localStorage.getItem('token');
    let userInfo = localStorage.getItem('userInfo') || '';
    const whiteList = ['/login', '/register', '/forgetPassword', '/companyLogin']; // 不重定向白名单
    if (!token) {
      if (whiteList.indexOf(to.path) !== -1) {
        next();
        return;
      }

      setTimeout(() => {
        router.push({
          path: '/login',
        });
      }, 10);
    }
    userInfo = userInfo && JSON.parse(localStorage.getItem('userInfo') || '');
    if (token && to.path === '/comprehensiveCensus' && userInfo.role_name !== '超级管理员') {
      ElMessage.warning('当前用户暂无权限');
    } else {
      next();
    }
  }
});
export default router;
